﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class User_Shares_DefineDeadLines : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Lblstatus.Text = "";
            if (DataListPreviousStep.SelectedItem.ItemIndex < 0)
            {
                Session["previousestepid"] = null;
            }
            else
            {


                Label lblpriority = (Label)DataListPreviousStep.SelectedItem.FindControl("Label14");
                Label lblhasrepeate = (Label)DataListPreviousStep.SelectedItem.FindControl("Label15");
                Session["previousestepid"] = lblpriority.Text;
                if (lblhasrepeate.Text == "True")
                {
                    TxtRepeat.Visible = true;
                    Label3.Visible = true;
                }
                else
                {
                    Label3.Visible = false;
                    TxtRepeat.Visible = false;
                }

                if (RadioButtonList1.SelectedValue == "0")
                {
                    SubSonic.Query qad = DataAccess.TblStepsAdvisor.CreateQuery();
                    qad.QueryType = SubSonic.QueryType.Select;
                    qad.BETWEEN_VALUES("Priority", Convert.ToInt32(lblpriority.Text), 18);
                    qad.OrderBy = SubSonic.OrderBy.Asc("Priority");

                    DataListSteps.DataSource = qad.ExecuteReader();
                    DataListSteps.DataBind();
                }
                if (RadioButtonList1.SelectedValue == "1")
                {
                    SubSonic.Query qad = DataAccess.TblStepsSurveyor.CreateQuery();
                    qad.QueryType = SubSonic.QueryType.Select;
                    qad.BETWEEN_VALUES("Priority", Convert.ToInt32(lblpriority.Text), 8);
                    qad.OrderBy = SubSonic.OrderBy.Asc("Priority");

                    DataListSteps.DataSource = qad.ExecuteReader();
                    DataListSteps.DataBind();
                }

            }
        }
        catch
        {
            Lblstatus.Visible = false;
           Lblstatusfaile.CssClass = "error";
            Lblstatusfaile.Text = "خطا در بانک اطلاعاتی لطفا مجددا سعی کنید ";
            
        }
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BtnSave.Visible = true;
            Label1.Visible = true;
            TxtDays.Visible = true;
            if (RadioButtonList1.SelectedValue == "0")
            {
                DataListPreviousStep.DataSource = DataAccess.TblStepsAdvisor.FetchAll(SubSonic.OrderBy.Asc("Priority"));
                DataListPreviousStep.DataBind();
            }
            if (RadioButtonList1.SelectedValue == "1")
            {
                DataListPreviousStep.DataSource = DataAccess.TblStepsSurveyor.FetchAll(SubSonic.OrderBy.Asc("Priority"));
                DataListPreviousStep.DataBind();
            }
        }
        catch
        {
            Lblstatus.Visible = false;
            Lblstatusfaile.CssClass = "error";
            Lblstatusfaile.Text = "خطا در بانک اطلاعاتی لطفا مجددا سعی کنید ";
        }
    }
 
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
          
                if (RadioButtonList1.SelectedValue == "0")
                {
                    SubSonic.Query update = DataAccess.TblStepsAdvisor.CreateQuery();
                    update.QueryType = SubSonic.QueryType.Update;
                    if (TxtDays.Text != "" )
                    {
                    update.AddUpdateSetting(DataAccess.TblStepsAdvisor.Columns.DeadLineDays, TxtDays.Text);
                    update.AddUpdateSetting(DataAccess.TblStepsAdvisor.Columns.HasDeadLine,true);
                    }
                    if (TxtRepeat.Text != "")
                    {
                    update.AddUpdateSetting(DataAccess.TblStepsAdvisor.Columns.RepeatCount, TxtRepeat.Text);
                    update.AddUpdateSetting(DataAccess.TblStepsAdvisor.Columns.HasRepeat, true);
                    }
                    update.AddUpdateSetting(DataAccess.TblStepsAdvisor.Columns.PreviousStep, Session["previousestepid"].ToString());
                    update.AddWhere(DataAccess.TblStepsAdvisor.Columns.Id, DataListSteps.SelectedValue);
                    update.Execute();
                    Lblstatusfaile.Visible = false;
                    Lblstatus.Text = ".مدت زمان محاسبه تاخیر و تعداد دفعات تکرار ثبت شد ";
                    TxtDays.Text = "";
                }
                if (RadioButtonList1.SelectedValue == "1")
                {
                    SubSonic.Query update = DataAccess.TblStepsSurveyor.CreateQuery();
                    update.QueryType = SubSonic.QueryType.Update;
                    if (TxtDays.Text != "")
                    {
                        update.AddUpdateSetting(DataAccess.TblStepsSurveyor.Columns.DeadLineDays, TxtDays.Text);
                        update.AddUpdateSetting(DataAccess.TblStepsSurveyor.Columns.HasDeadLine, true);
                    }
                    if (TxtRepeat.Text != "")
                    {
                        update.AddUpdateSetting(DataAccess.TblStepsSurveyor.Columns.RepeatCount, TxtRepeat.Text);
                        update.AddUpdateSetting(DataAccess.TblStepsSurveyor.Columns.HasRepeat, true);
                    }
                    update.AddUpdateSetting(DataAccess.TblStepsSurveyor.Columns.PreviousStep, Session["previousestepid"].ToString());
                    update.AddWhere(DataAccess.TblStepsSurveyor.Columns.Id, DataListSteps.SelectedValue);
                    update.Execute();
                    Lblstatusfaile.Visible = false;
                    Lblstatus.Text = ".مدت زمان محاسبه تاخیر و تعداد دفعات تکرار ثبت شد ";
                    TxtDays.Text = "";
                }
            
        }
         catch
        {
            Lblstatus.Visible = false;
            Lblstatusfaile.Text = "ثبت با خطا مواجه شد. ";
                 
         }
      }

    protected void DataListStep_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (RadioButtonList1.SelectedValue == "0")
            {
                SubSonic.Query qad = DataAccess.TblStepsAdvisor.CreateQuery();
                qad.QueryType = SubSonic.QueryType.Select;
                qad.AddWhere(DataAccess.TblStepsAdvisor.Columns.Id, DataListSteps.SelectedValue);
                qad.AddWhere(DataAccess.TblStepsAdvisor.Columns.PreviousStep, Session["previousestepid"]);

                qad.OrderBy = SubSonic.OrderBy.Asc("Priority");
                IDataReader dr = qad.ExecuteReader();
                if (dr.Read())
                {

                    TxtDays.Text = dr["DeadLineDays"].ToString();
                    TxtRepeat.Text = dr["RepeatCount"].ToString();
                    dr.Close();
                }
                else
                {
                    TxtDays.Text = "";
                    TxtRepeat.Text ="";
                }
            }
            if (RadioButtonList1.SelectedValue == "1")
            {
                SubSonic.Query qad = DataAccess.TblStepsSurveyor.CreateQuery();
                qad.QueryType = SubSonic.QueryType.Select;
                qad.AddWhere(DataAccess.TblStepsSurveyor.Columns.Id, DataListSteps.SelectedValue);
                qad.AddWhere(DataAccess.TblStepsSurveyor.Columns.PreviousStep, Session["previousestepid"]);

                qad.OrderBy = SubSonic.OrderBy.Asc("Priority");
                IDataReader dr = qad.ExecuteReader();
              
            
                if (dr.Read())
                {

                    TxtDays.Text = dr["DeadLineDays"].ToString();
                    TxtRepeat.Text = dr["RepeatCount"].ToString();
                    dr.Close();
                }
                else
                {
                    TxtDays.Text = "";
                    TxtRepeat.Text = "";
                }
            }
        }
        catch
        {
 Lblstatus.Visible = false;
 Lblstatusfaile.CssClass = "error";
 Lblstatusfaile.Text = "خطا در بانک اطلاعاتی لطفا مجددا سعی کنید ";
      
        }
    }
}